package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtil;
import com.triones.ljr.entity.dto.CollectDO;
import com.triones.ljr.entity.dto.EnterpriseDO;
import com.triones.ljr.entity.dto.PolicyDO;
import com.triones.ljr.entity.vo.*;
import com.triones.ljr.mapper.CollectMapper;
import com.triones.ljr.mapper.PolicyMapper;
import com.triones.ljr.service.CollectService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户政策收藏
 */
@Service
@RequiredArgsConstructor
public class CollectServiceImpl implements CollectService {

    private final CollectMapper collectMapper;

    private final PolicyMapper policyMapper;

    @Override
    public TableDataInfo<CustomCollectVO> pageList(CollectDO dto, PageQuery pageQuery) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper
            .eq(ObjectUtil.isNotNull(dto.getPId()),"tc.p_id",dto.getPId())
            .orderByDesc("tc.create_time");
        Page<CustomCollectVO> result = collectMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean collect(Long pId, Integer status) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        PolicyVO policyVO = policyMapper.selectVoOne(new LambdaQueryWrapper<PolicyDO>()
            .eq(PolicyDO::getId,pId)
            .eq(PolicyDO::getIsDel, "0")
            .eq(PolicyDO::getStatus, "0"));
        if (policyVO == null) {
            throw new SecurityException("没有查询到有效的政策信息");
        }
        CollectDO collectDO = collectMapper.selectOne(new LambdaQueryWrapper<CollectDO>()
            .eq(CollectDO::getPId, pId).eq(CollectDO::getWxuId, loginUser.getUserId()));
        if (status == 1) {
            if (collectDO == null) {
                collectDO = new CollectDO();
                collectDO.setPId(pId);
                collectDO.setWxuId(loginUser.getUserId());
                collectDO.setCreateTime(DateUtil.getDateTimeString());
                collectMapper.insert(collectDO);
            }
        }
        if (status == 0) {
            if (collectDO != null) {
                collectMapper.deleteById(collectDO);
            }
        }
        return true;
    }

    @Override
    public TableDataInfo<CustomApiCollectVO> apiCollectPageList(String status, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("tc.wxu_id",loginUser.getUserId()).orderByDesc("tc.create_time");
        Page<CustomApiCollectVO> result = collectMapper.apiCollectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }
}
